package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insertSetmeal(setmeal);
        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        for (SetmealDish dish : list) {
            dish.setSetmealId(setmeal.getId());
            setmealMapper.insertSD(dish);
        }

    }

    @Override
    public PageResult pageQurey(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<Setmeal> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());
        return pageResult;
    }

    @Transactional
    @Override
    public void deleteSetmeal(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        for (Long id : ids) {
            setmealMapper.deleteSetmeal(id);
            setmealMapper.deleteSD(id);
        }
    }

    @Transactional
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        Long sid = setmealDTO.getId();
        setmealDishMapper.deleteSD(sid);
        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : list) {
            setmealDish.setSetmealId(sid);
            setmealDishMapper.insertSD(setmealDish);
        }
        setmealMapper.updateSetmeal(setmeal);
    }

    @Override
    public SetmealVO selectByVO(Long id) {
        SetmealVO setmealVO = setmealMapper.selectByVO(id);
        List<SetmealDish> list = setmealMapper.selectByD(id);
        setmealVO.setSetmealDishes(list);
        return setmealVO;
    }

    @Transactional
    @Override
    public void updateStatus(Integer status,Long id) {
        List<SetmealDish> list= setmealDishMapper.selectBySDStatus(id);
        for (SetmealDish setmealDish : list) {
            Long dishid = setmealDish.getDishId();
            Dish dishes = setmealDishMapper.selectBYDStatus(dishid);
            if (dishes.getStatus() == StatusConstant.DISABLE){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        setmealMapper.updateStatus(status,id);
    }

    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> slist = setmealMapper.list(setmeal);
        return slist;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
